package com.wjs.gulimall.thread;

import java.util.concurrent.*;
import java.util.function.Consumer;

public class ThreadTest {
    public static final ExecutorService service=Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("开始.................");
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程号" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("线程结果:" + i);
//        }, service);
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程号" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("线程结果:" + i);
//            return i;
//        }, service).whenComplete((result,exception)->{
//            System.out.println("异步任务完成，结果是"+result+"异常是"+exception);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程号" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("线程结果:" + i);
//            return i;
//        }, service).handle((res,thr)->{
//            if(res!=null){
//                return res*2;
//            }
//            if(thr!=null){
//                return 0;
//            }
//            return 0;
//        });
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程号" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("线程结果:" + i);
//            return i;
//        }, service).thenApplyAsync(r -> {
//            System.out.println("任务2启动了" + r);
//            return r + "wjs";
//        }, service);

//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务一结束");
//            return i;
//        }, service);
//
//        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务二" + Thread.currentThread().getId());
//            System.out.println("任务二结束" );
//            return "HELLO";
//        },service);

//       future1.thenAcceptBothAsync(future2,(f1,f2)->{
//           System.out.println("任务三开启了"+f1+":"+f2);
//       },service);

//        CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
//            System.out.println("任务三开启");
//            return f1 + ":" + f2;
//        }, service);
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        },service);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        },service);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println("查询商品介绍");
            return "华为";
        },service);
//        CompletableFuture<Void> future = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        CompletableFuture<Object> future = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        Object o = future.get();
        System.out.println("所有任务结束"+o.toString());
    }

    public static class Thread01 extends Thread{
        @Override
        public synchronized void start() {
            System.out.println("当前线程号"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("线程结果:"+i);
        }
    }

    public static class Runnable1 implements Runnable{

        @Override
        public void run() {
            System.out.println("当前线程号"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("线程结果:"+i);
        }
    }

    public static class Callable1 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程号"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("线程结果:"+i);
            return i;
        }
    }
}
